import {getKnowledgeList , deleteKnowledgeList , crateData ,  editData  ,publish} from '@/api/knowledge'
import { PAYLOAD , ITEMTYPE , KNOWLEDGEALLSTORE} from '../type/knowledge'
import { message } from 'antd'
export default {
    namespace:"knowledge",
    state:{
        knowledgeList:[],
        knowledgeTotel:0,
    },
    reducers:{
        setKnowledgeList(state:KNOWLEDGEALLSTORE,{payload}:PAYLOAD) {
          state.knowledgeList = payload.knowledgeList.data.data[0]   
          state.knowledgeTotel =  payload.knowledgeList.data.data[1]  
        },
        delKnowledgeList(state:KNOWLEDGEALLSTORE , {payload}:PAYLOAD){
            state.knowledgeList.forEach((item:ITEMTYPE,index:number) => {
                if(item.id === payload.id){
                    state.knowledgeList.splice(index,1)
                }
            })
            message.success('操作成功')
        },
        //新建数据
        newCreate(state:KNOWLEDGEALLSTORE , {payload}:PAYLOAD) {           
           state.knowledgeList.unshift(payload.values)
           message.success('操作成功')
        },
        //编辑数据
        setEdit(state:KNOWLEDGEALLSTORE , {payload}:PAYLOAD) {
           console.log(payload,"5959");
           
          state.knowledgeList.forEach((item:ITEMTYPE,index:number) => {
            if(item.id === payload.id) {
                item.title = payload.values.title
                item.summary = payload.values.summary
                item.cover = payload.values.cover
                item.isCommentable = payload.values.isCommentable
            }
          })
        },
        //改变发布线上
        changePublish(state:KNOWLEDGEALLSTORE , {payload}:PAYLOAD) {
            state.knowledgeList.forEach((item:ITEMTYPE,index:number) => {
                if(item.id === payload.id) {
                    item.status = payload.status
                }
            })
        }


    },
    effects:{
        *getKnowledgeList({payload}:PAYLOAD,{call,put}:any) {
            let result:Response = yield call(getKnowledgeList,payload.page,payload.pageSize,payload.values.title,payload.values.status)
            yield put({type:"setKnowledgeList" , payload:{knowledgeList:result}})
        },
        *deleteKnowledgeList({payload}:PAYLOAD , {call,put}:any) {
            let result:Response = yield call(deleteKnowledgeList,payload.id)
            yield put({type:"delKnowledgeList",payload:{ knowledgeList:result ,id:payload.id}})

        },
        //新建数据
        *crateData({payload}:PAYLOAD , {call,put}:any) {
            let result:Response = yield call(crateData , payload.values)
            yield put({type:"newCreate",payload:{ knowledgeList:result,values:payload.values}})
        },
        //编辑数据
        *editData({payload}:PAYLOAD , {call,put}:any) {
          let result:Response = yield call(editData , payload.id,payload.values)
          yield put({type:"setEdit" , payload:{knowledgeList:result,id:payload.id,values:payload.values}})
        },
        //发布线上
        *publish({payload}:PAYLOAD , {call,put}:any) {
          let result:Response = yield call(publish ,payload.id,payload.status)
          yield put({type:"changePublish" , payload:{knowledgeList:result,id:payload.id,status:payload.status}})  
        }


    }

}